/*    */ package xin.petstore.framework.utils;
/*    */ 
/*    */ import org.apache.commons.lang3.Validate;
/*    */ import org.slf4j.Logger;
/*    */ import org.slf4j.LoggerFactory;
/*    */ import org.springframework.beans.factory.DisposableBean;
/*    */ import org.springframework.context.ApplicationContext;
/*    */ import org.springframework.context.ApplicationContextAware;
/*    */ 
/*    */ public class SpringContextHolder
/*    */   implements ApplicationContextAware, DisposableBean
/*    */ {
/* 13 */   private static ApplicationContext applicationContext = null;
/*    */ 
/* 15 */   private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);
/*    */ 
/*    */   public static ApplicationContext getApplicationContext()
/*    */   {
/* 21 */     assertContextInjected();
/* 22 */     return applicationContext;
/*    */   }
/*    */ 
/*    */   public static <T> T getBean(String name)
/*    */   {
/* 29 */     assertContextInjected();
/* 30 */     return applicationContext.getBean(name);
/*    */   }
/*    */ 
/*    */   public static <T> T getBean(Class<T> requiredType)
/*    */   {
/* 37 */     assertContextInjected();
/* 38 */     return applicationContext.getBean(requiredType);
/*    */   }
/*    */ 
/*    */   public static void clearHolder()
/*    */   {
/* 45 */     logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
/* 46 */     applicationContext = null;
/*    */   }
/*    */ 
/*    */   public void setApplicationContext(ApplicationContext applicationContext)
/*    */   {
/* 53 */     logger.info("注入ApplicationContext到SpringContextHolder:{}", applicationContext);
/* 54 */     if (applicationContext != null) {
/* 55 */       logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + 
/* 56 */         applicationContext);
/*    */     }
/*    */ 
/* 59 */     applicationContext = applicationContext;
/*    */   }
/*    */ 
/*    */   public void destroy()
/*    */     throws Exception
/*    */   {
/* 66 */     clearHolder();
/*    */   }
/*    */ 
/*    */   private static void assertContextInjected()
/*    */   {
/* 73 */     Validate.validState(applicationContext != null, 
/* 74 */       "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.", new Object[0]);
/*    */   }
/*    */ }

/* Location:           C:\java代码反编译器\core-1.1.4.jar
 * Qualified Name:     com.eloancn.framework.utils.SpringContextHolder
 * JD-Core Version:    0.6.0
 */